package com.kate.mall.promotion.service.impl;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.Query;

import com.kate.mall.promotion.dao.AdDao;
import com.kate.mall.promotion.entity.AdEntity;
import com.kate.mall.promotion.service.AdService;


@Service("adService")
public class AdServiceImpl extends ServiceImpl<AdDao, AdEntity> implements AdService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdEntity> page = this.page(
                new Query<AdEntity>().getPage(params),
                generatorWrapper(params)
        );

        return new PageUtils(page);
    }
    private  QueryWrapper<AdEntity> generatorWrapper(Map<String, Object> params){
        QueryWrapper<AdEntity> queryWrapper = new QueryWrapper<>();
        //TODO 查询未结束的广告
        queryWrapper.lambda().gt(AdEntity::getEndTime, LocalDateTime.now()).lt(AdEntity::getStartTime,LocalDateTime.now());
        //TODO 广告活动已经启动的
        queryWrapper.lambda().eq(AdEntity::getEnabled,true);
        //TODO 位置在首页的
        if(String.valueOf(params.get("position")).equals("1")){
            queryWrapper.lambda().eq(AdEntity::getPosition,1);
        }
        return queryWrapper;
    }

}